#!/bin/bash

### Initialize variables
	uploaddir="upload"
	maxwidth=1024
	maxwidththumb=200
	maxheight=768
	maxheightthumb=150
	
###Rename files to remove spaces (replaces spaces with '_' )

	for f in *; do
	file=$(echo $f | tr ' ' _)
	[ ! -f $file ] && mv "$f" $file
	done

###Resize originals to uploadable images

	#create directories
	mkdir -p $uploaddir/thumbs $uploaddir/pics

	for line in `ls | grep -i "\."jpg$`
	do
		##get width and height of image
	
		#sets $width to width of image in pixels
		width=`identify -format "%[fx:w]" $line`
	
		#sets $height to height of image in pixels
		height=`identify -format "%[fx:h]" $line`
	
		##figure out if image is portrait or landscape


		if [ $width -gt $height ]
		then
			#if landscape, resize as landscape
			convert -resize "$maxwidth"x"$maxheight" $line $uploaddir/pics/$line
			convert -resize "$maxwidththumb"x"$maxheightthumb" $line $uploaddir/thumbs/$line
		else
			#if portrait, resize as portrait
			convert -resize "$maxheight"x"$maxwidth" $line $uploaddir/pics/$line
			convert -resize "$maxheightthumb"x"$maxwidththumb" $line $uploaddir/thumbs/$line
		fi
	done